package cn.k.controller;

import cn.k.domain.SystemMenu;
import cn.k.qo.JSONResult;
import cn.k.qo.QueryObject;
import cn.k.qo.SystemMenuQueryObject;
import cn.k.service.ISystemMenuService;
import cn.k.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/systemMenu")
public class SystemMenuController {

    @Autowired
    private ISystemMenuService systemMenuService;

    //处理分页查询页面请求
    @RequestMapping("/list")
    @RequirePermission(name = "菜单查询",expression = "systemMenu:list")
    public String list(@ModelAttribute("qo") SystemMenuQueryObject qo, Model model){
        PageInfo<SystemMenu> pageInfo = systemMenuService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        //查询上级菜单
        List<SystemMenu> menu=systemMenuService.queryMenu(qo);
        model.addAttribute("menu",menu);
        return "systemMenu/list";
    }


    //处理删除请求
    @RequirePermission(name = "菜单删除",expression = "systemMenu:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id!=null){
            systemMenuService.delete(id);
        }
        return "redirect:/systemMenu/list";
    }


    //处理添加或者更新的请求
    @RequirePermission(name ="菜单编辑",expression = "systemMenu:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(SystemMenu systemMenu){

        //判断有没有id
        if (systemMenu.getId()==null){
            systemMenuService.insert(systemMenu);
        }else {
            systemMenuService.update(systemMenu);
        }
        return "redirect:/systemMenu/list";
    }

    //处理导航栏菜单
    @RequestMapping("/menu")
    @ResponseBody
    public List<Map> menu(){
        return systemMenuService.queryByMenu();
    }


    //updateStatus
    @RequestMapping("/updateStatus")
    public String updateStatus(Long id,int status){
         systemMenuService.updateStatus(id,status);
        return "redirect:/systemMenu/list";
    }



}
